<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :search.sync="search"
      @row-del="rowDel"
      @row-update="rowUpdate"
      :before-open="beforeOpen"
      :page="page"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template #menuRight="{ row }">
        <el-button type="primary" size="small" @click="addRow"
          >新增优惠充值</el-button
        >
      </template>
    </avue-crud>
    <el-dialog
      title="新增优惠充值"
      append-to-body
      :visible.sync="addDialog"
      width="600px"
    >
      <preferential-top-up-add @closeAddDialog="closeAddDialog" />
    </el-dialog>
  </basic-container>
</template>

<script>
import {
  getList,
  remove,
  submit,
} from "@/api/marketing-management/preferntial-top-up-management";
import { mapGetters } from "vuex";
import PreferentialTopUpAdd from "@/views/marketing-management/preferential-top-up-add.vue";

export default {
  data() {
    return {
      form: {},
      search: {},
      roleBox: false,
      excelBox: false,
      loading: true,
      selectionList: [],
      query: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      props: {
        label: "title",
        value: "key",
      },
      option: {
        columnBtn: false,
        refreshBtn: false,
        height: "auto",
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: false,
        addBtn: false,
        dialogWidth: "800px",
        labelWidth: "150px",
        align: "center",
        column: [
          {
            label: "充值金额（元）",
            prop: "rechargeMoney",
            width: 130,
            span: 24,
          },
          {
            label: "赠送金额（元）",
            width: 130,
            prop: "sendMoney",
            span: 24,
          },
          {
            label: "返现金额（元）",
            prop: "returnMoney",
            width: 130,
            span: 24,
          },
          {
            label: "推广员奖励金额（元）",
            width: 180,
            prop: "shareMoney",
            span: 24,
          },
        ],
      },
      data: [],
      addDialog: false,
    };
  },
  components: {
    PreferentialTopUpAdd,
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
  },
  methods: {
    closeAddDialog(type) {
      this.addDialog = false;
      if (type === "refresh") {
        this.onLoad(this.page);
      }
    },
    addRow() {
      this.addDialog = true;
    },
    rowUpdate(row, index, done, loading) {
      submit(row).then(
        () => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    beforeOpen(done, type) {
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
  },
};
</script>

<style></style>
